﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once

#include "ICommonEditAlignManager.h"
#include "UniIdentity.h"
#include "IElement.h"

namespace gcmp
{
    template<class ElementClass, class AlignStrategyClass>
    class ElementAlignStrategyRegister
    {
    public:
        ElementAlignStrategyRegister()
        {
            auto* pMgr = ICommonEditAlignManager::Get();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pMgr, L"pMgr为空", L"GMEP", L"2020-12-22");

            const UniIdentity& implUid = ElementClass::GetImplementationUid();
            IElementAlignStrategy* pAlignStrategy = pMgr->GetStrategy(implUid);
            if (!pAlignStrategy)
            {
                OwnerPtr<IElementAlignStrategy> opAlignStrategy = NEW_AS_OWNER_PTR(AlignStrategyClass);
                pMgr->AddStrategy(implUid, TransferOwnership(opAlignStrategy));
            }
        }

        ~ElementAlignStrategyRegister() {}
    };
}

#define REGISTER_ELEMENT_ALIGN_STRATEGY(ELEMENT_CLASS, ALIGN_STRATEGY_CLASS) \
namespace { \
    static gcmp::ElementAlignStrategyRegister<ELEMENT_CLASS, ALIGN_STRATEGY_CLASS> \
        s_##ELEMENT_CLASS##ALIGN_STRATEGY_CLASS##ElementAlignStrategyRegister; \
}

